﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using compshop.web.CompShopDataService;
using System.Text;


namespace compshop.web
{
    public class ObjectDataSource 
    {
        public static CompShopEntities csEntities;
 
        static ObjectDataSource()
        {
            csEntities = new CompShopEntities(new Uri(URIs.ServiceURI));
        }

        #region Store Procedures

        public static int AddProductFromSupplier(int genProductID, int count, int department, System.DateTime shipDate, System.Decimal purchasePrice)
        {
            string strParams = String.Format("?genProductID={0}&count={1}&department={2}&shipDate={3}&purchasePrice={4}",
                genProductID.ToString(), count.ToString(), department.ToString(), shipDate.ToShortDateString(), purchasePrice.ToString());
            int res = 0;
            try
            {
                res = csEntities.Execute<int>(new Uri(URIs.AddProductFromSupplierURI + strParams)).First();
            }
            catch (Exception e)
            {
                ///
                return 0;
            }
            return res;
        }

        public static int AddOrder(int orderID, int productID, int count)
        {
            string strParams = String.Format("?orderID={0}&productID={1}&count={2}",
                orderID.ToString(), productID.ToString(), count.ToString());
            int res = 0;
            try
            {
                string str = URIs.AddProductToOrderURI + strParams;
                res = csEntities.Execute<int>(new Uri(str)).First();
            }
            catch (Exception e)
            {
                return -1;
            }
            return res;
        }
        #endregion

        #region Orders

        public void MakeOrder(ref ClientOrder order)
        {
            csEntities.AddToClientOrders(order);
            csEntities.SaveChanges();
        }

        #endregion


        #region GeneralProducts

        public List<GeneralProduct> GetGeneralProduct(int category, int subCategory, int producer)
        {
            List<GeneralProduct> list;
            if (subCategory >= 0)
            {
                //list = (from p in csEntities.GeneralProducts
                //        where p.SubCategoryID == subCategory
                //        select p).ToList();
                list = csEntities.GeneralProducts.Expand("Producer,Country").
                    Where(x => x.SubCategoryID == subCategory).ToList<GeneralProduct>();
            }
            else
            {
                if (category >= 0)
                {
                    //list = (from p in csEntities.GeneralProducts
                    //        where p.SubCategory.CategoryID == category
                    //        select p).ToList();
                    list = csEntities.GeneralProducts.Expand("Producer,Country").
                        Where(x => x.SubCategory.CategoryID == category).ToList<GeneralProduct>();
                }
                else
                {
                    //list = (from p in csEntities.GeneralProducts
                    //        select p).ToList();
                    list = csEntities.GeneralProducts.Expand("Producer,Country").ToList<GeneralProduct>();
                }
            }
            return list;
        }

        public static GeneralProduct GetGeneralProductById(int id)
        {
            GeneralProduct gp = csEntities.GeneralProducts.Expand("Producer,SubCategory").
                Where(x=> x.GeneralProductID==id).First();
            return gp;
            //return (from c in csEntities.GeneralProducts
            //        where c.GeneralProductID == id
            //        select c).First();
        }

        public static List<GeneralProductParameter> GetParameters(int id)
        {
            if (id > 0)
                return csEntities.GeneralProductParameters.Expand("Parameter,GeneralProduct").
                            Where(x => x.GeneralProductID == id).ToList<GeneralProductParameter>();
            else
                return null;
        }

        #endregion
    }
}